package com.buildingsell.interceptor;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ls
 * @category
 *
 */

public class AuthorInterceptor implements HandlerInterceptor {

    Logger logger = Logger.getLogger(AuthorInterceptor.class);
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        logger.info("特殊拦截处理，=====前=====");
        String path = httpServletRequest.getRequestURI();
        String firstAuthor = method(path,1);
        String secondAuthor = method(path,2);
        boolean flag = false;
         //获取session
        HttpSession session = httpServletRequest.getSession();
        //从session中获取信息
        Object obj = session.getAttribute("userAndCompanyVO");

        return flag;
    }

    public static String method(String path, int i){
            int firstIndex = path.indexOf("/", 1);
            path = path.substring(firstIndex + 1);
            String firstAuthor = path.substring(0, path.indexOf("/"));
            System.out.println(firstAuthor);

            int start = path.indexOf("/");
            int end = path.indexOf("/", start + 1);
            String secondAuthor = path.substring(start + 1, end);
            System.out.println(secondAuthor);

            if(i==1){
                return firstAuthor;
            }else{
                return secondAuthor;
            }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
